<?php

namespace app\admin\service;

use app\admin\model\UM as UM;
use app\common\model\AccountBill;
use app\common\service\MarginService;
use ShidianUtil\Exception\DataNotExistException;
use app\admin\dao\MarginDao;
use app\common\model\Margin;
use think\Collection;
use think\exception\ValidateException;

/**
 * Class MarginPunishService
 * @package app\admin\service
 */
class MarginPunishService
{
    // dao
    private MarginDao $marginDao;

    public function __construct(MarginDao $marginDao)
    {
        $this->marginDao = $marginDao;
    }

    /**
     * 更新
     * @param UM  $user
     * @param int   $id
     * @param array $params
     * @return void
     */
    public function modify(UM $user, int $id, array $params): void
    {
        $status = $params['status'] ?? 1;
        $status = $status == 1 ? 1 : 2;

        //这里如果要修改查询，建议把查询就放到这里
        $record = Margin::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该信息不存在或已被删除');
        }

        if($record->status != 0){
            throw new ValidateException('请勿重复操作');
        }

        if($status == 1){
            $this->getMarginService()->punish($record);
        }else{
            $record->status = $status;
            $record->save();
        }
    }

    private function getMarginService():MarginService{
        return app(MarginService::class);
    }
}
